package com.vcredit.creditcat.manager;

import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IAlipayProcessManager;
import com.vcredit.creditcat.api.IAuthAccumulationManager;
import com.vcredit.creditcat.api.ICustProductApplyManager;
import com.vcredit.creditcat.api.IJdProcessManager;
import com.vcredit.creditcat.api.IMobileProcessManager;
import com.vcredit.creditcat.api.ITaoBaoProcessManager;
import com.vcredit.creditcat.model.AuthAccumulation;
import com.vcredit.creditcat.model.AuthAlipay;
import com.vcredit.creditcat.model.AuthJd;
import com.vcredit.creditcat.model.AuthMobile;
import com.vcredit.creditcat.model.AuthSocial;
import com.vcredit.creditcat.model.AuthTaobao;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.base.AbstractItem;

@Component
public class TimeManager<T extends AbstractItem> {

  @Reference(version = "1.0")
  private IMobileProcessManager mobileProcessManager;

  @Reference(version = "1.0")
  private IAlipayProcessManager alipayProcessManager;

  @Reference(version = "1.0")
  private IJdProcessManager jdProcessManager;

  @Reference(version = "1.0")
  private ITaoBaoProcessManager taoBaoProcessManager;

  @Reference(version = "1.0")
  private IAuthAccumulationManager authAccumulationManager;

  @Reference(version = "1.0")
  private ICustProductApplyManager custProductApplyRecordManager;

  @SuppressWarnings("unchecked")
  public List<T> findUnCrawl(Class<T> clazz) {
    if (clazz.getName().equals(AuthMobile.class.getName())) {
      return (List<T>) mobileProcessManager.findUnCrawl();
    } else if (clazz.getName().equals(AuthJd.class.getName())) {
      return (List<T>) jdProcessManager.crawlJd();
    } else if (clazz.getName().equals(AuthTaobao.class.getName())) {
      return (List<T>) taoBaoProcessManager.findUnCrawl();
    } else if (clazz.getName().equals(AuthAlipay.class.getName())) {
      return (List<T>) alipayProcessManager.crawlAlipay();
    } else if (clazz.getName().equals(AuthAccumulation.class.getName())) {
      return (List<T>) authAccumulationManager.findUnCrawlAccumulation();
    } else if (clazz.getName().equals(AuthSocial.class.getName())) {
      return (List<T>) authAccumulationManager.findAlreadyLoginAuthSocial();
    } else if (clazz.getName().equals(CustProductApply.class.getName())) {
      return (List<T>) custProductApplyRecordManager.findRequireQuryLimitList();
    }
    return null;
  }

  public void updateCrawlerState(T t) throws Exception {
    if (t instanceof AuthMobile) {
      mobileProcessManager.updateCrawlerState((AuthMobile) t);
    } else if (t instanceof AuthJd) {
      jdProcessManager.updateCrawlStatus((AuthJd) t);
    }
    if (t instanceof AuthSocial) {
      authAccumulationManager.updateSocialCrawlState((AuthSocial) t);
    }
    if (t instanceof AuthTaobao) {
      taoBaoProcessManager.updateCrawlStatus((AuthTaobao) t);
    }
    if (t instanceof AuthAlipay) {
      alipayProcessManager.updateCrawlState((AuthAlipay) t);
    }
    if (t instanceof AuthAccumulation) {
      authAccumulationManager.updateAccumulationCrawlerState((AuthAccumulation) t);
    }
    if (t instanceof CustProductApply) {
      custProductApplyRecordManager.queryLimitThatWaitLongTimeAndNoResponse((CustProductApply) t);
    }

  }

  public IAuthAccumulationManager getAuthAccumulationManager() {
    return authAccumulationManager;
  }

}
